package cn.hua.trip.admin.controller;

import cn.hua.trip.admin.util.UploadUtil;
import cn.hua.trip.base.domain.Region;
import cn.hua.trip.base.domain.TravelCommend;
import cn.hua.trip.base.query.TravelCommendQueryObject;
import cn.hua.trip.base.service.ITravelCommendService;
import cn.hua.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {
    @Autowired
    private ITravelCommendService travelCommendService;

    //游记推荐管理
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelCommendQueryObject qo) {
        model.addAttribute("pageInfo", travelCommendService.query(qo));
        return "/travelCommend/list";
    }



    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(TravelCommend travelCommend, MultipartFile pic){
        JSONResult result = new JSONResult();
        if (pic!=null) {
            String imagePath = UploadUtil.upload(pic, UploadUtil.IMG_BASE_PATH);
            travelCommend.setCoverUrl(imagePath);
        }

        try {
            travelCommendService.saveOrUpdate(travelCommend);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
